package com.zhaobenquan.controller;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

/**
 * @author zhaobenquan
 * @Date 2021/7/2 9:53
 */

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    SimpleJobLauncher jobLauncher;

    @Autowired
    Job myJob;

    @PostMapping("/show")
    public String testJob1(@RequestParam(value = "file") MultipartFile uploadfile) throws IOException {
        Path path = saveUploadedFile(uploadfile);
        Path path1 = path.toAbsolutePath();
        String s1 = path1.toString();
        String s = uploadfile.toString();
        String originalFilename = uploadfile.getOriginalFilename();
        String name = uploadfile.getName();
        return "成功";
    }

    @GetMapping("/job")
    public void testJob(@RequestParam(value = "file") MultipartFile uploadfile,
                        @RequestParam(value = "groupId", required = false) Long groupId) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, IOException {
        //    后置参数：使用JobParameters中绑定参数 addLong  addString 等方法
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
        String uuid = UUID.randomUUID().toString();
        jobParametersBuilder.addLong("groupId", groupId)
                .addString("rawFileName", uploadfile.getOriginalFilename())
                .addString("uuid", uuid)
                .addString("ftpFileName",saveUploadedFile(uploadfile).toAbsolutePath().toString());
        try {
            JobExecution execution = jobLauncher.run(myJob, jobParametersBuilder.toJobParameters());
            System.out.println("sdsad");
        } catch (Exception e) {
            throw new RuntimeException("异常出现了");
        }

    }

    private Path saveUploadedFile(MultipartFile file) throws IOException {
        byte[] bytes = file.getBytes();
        String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        Path path = Paths.get("D:/java.io.tmpdir" + File.separator + UUID.randomUUID().toString() + suffixName);
        return Files.write(path, bytes);
    }

}
